package server;

import constants.ServerConstants;
import database.DatabaseConnection;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import tools.PropertyTool;

public class ServerProperties {

    private static final Properties props = new Properties();
    private static Properties settings = new Properties();
    private static PropertyTool propTool = new PropertyTool(new Properties());
    private static Map<String, Boolean> blockedOpcodes = new HashMap();
    private static boolean blockDefault;
    private static String[] blockSkills;
    private static final Properties skillProps = new Properties();
    private static boolean show = false;
    private static boolean showPacket = false;
    private static boolean lvkejian = true;
    public static boolean IsTrueMAC = true;
    public static boolean IsTrueIP = true;
    public static int[] 稀有的物品;
    private static final Properties 稀有物品 = new Properties();
    private static String[] 未修复的技能;
    private static final Properties 未修复技能 = new Properties();
    private static int max;
    private static int csm;

    public static void loadProperties(String s) {
        FileReader fr;
        try {
            fr = new FileReader(s);
            props.load(fr);
            fr.close();
        } catch (IOException ex) {
        }
    }

    public static void loadSkills() {
        try {
            FileReader sp = new FileReader("config/skills.properties");
            skillProps.load(sp);
            sp.close();
        } catch (IOException ex) {
            System.out.println("加载 skills.properties 配置出错 " + ex);
        }
        blockSkills = null;
        blockSkills = skillProps.getProperty("BlockSkills").split(",");
    }

    public static String[] getBlockSkills() {
        return blockSkills;
    }

    public static boolean getBlockSkills(int id) {
        String[] Str = getBlockSkills();
        String skillId = id + "";
        for (int i = 0; i < Str.length; i++) {
            if (Str[i].equals(skillId)) {
                System.out.println("禁止技能:" + skillId);
                return true;
            }
        }
        return false;
    }

    public static void 加载稀有物品数据() {
        try {
            FileReader sp = new FileReader("稀有物品.properties");
            稀有物品.load(sp);
            sp.close();
        } catch (IOException ex) {
            System.out.println("加载 稀有物品.properties 配置出错 " + ex);
        }
        稀有的物品 = null;

        System.out.println("共加载未修复技能 : " + 未修复的技能.length + "  个！");
    }

    public static void 加载未修复的技能数据() {
        //*去除验证
        if ((!ServerConstants.getIsTrue()) && (!Start.isTrue())) {
            System.exit(0);
        }

        try {
            FileReader sp = new FileReader("未修复技能.properties");
            未修复技能.load(sp);
            sp.close();
        } catch (IOException ex) {
            System.out.println("加载 未修复技能.properties 配置出错 " + ex);
        }
        未修复的技能 = null;
        未修复的技能 = 未修复技能.getProperty("未修复技能").split(",");
        System.out.println("共加载未修复技能 : " + 未修复的技能.length + "  个！");
    }

    public static String[] 未修复的技能() {
        return 未修复的技能;
    }

    public static boolean 是否是未修复技能(int id) {
        String[] Str = 未修复的技能();
        String skillId = id + "";
        for (int i = 0; i < Str.length; i++) {
            if (Str[i].equals(skillId)) {
                return true;
            }
        }
        return false;
    }

    public static void loadSettings() {
        try {
            FileInputStream fis = new FileInputStream("settings.properties");
            settings.load(fis);
            fis.close();
        } catch (IOException ex) {
            System.out.println("加载 settings.properties 配置出错" + ex);
        }
        propTool = new PropertyTool(settings);
        showPacket = propTool.getSettingInt("ShowPacket", 1) > 0;
        blockDefault = propTool.getSettingInt("BlockDefault", 0) > 0;
        csm = propTool.getSettingInt("csm", 0);
        max = propTool.getSettingInt("max", 0);
        blockedOpcodes.clear();
        for (Map.Entry entry : settings.entrySet()) {
            String property = (String) entry.getKey();
            if ((property.startsWith("S_")) || (property.startsWith("R_"))) {
                blockedOpcodes.put(property, Boolean.valueOf(propTool.getSettingInt(property, 0) > 0));
            }
        }
    }

    public static boolean isLoadShow() {
        if (IsLvkejian()) {
            show = true;
        }
        return show;
    }

    public static boolean ShowPacket() {
        if ((Boolean.parseBoolean(props.getProperty("world.ShowPacket", "false")))) {
            show = true;
            showPacket = true;
        }
        return (showPacket) && (show);
    }

    public static boolean SendPacket(String op, String pHeaderStr) {
        if (op.equals("UNKNOWN")) {
            return blockedOpcodes.containsKey("S_" + pHeaderStr) ? (blockedOpcodes.get("S_" + pHeaderStr)) : blockDefault;
        }
        return blockedOpcodes.containsKey("S_" + op) ? (blockedOpcodes.get("S_" + op)) : blockDefault;
    }
    
    public static boolean RecvPacket(String op, String pHeaderStr) {
        if (op.equals("UNKNOWN")) {
            return blockedOpcodes.containsKey("R_" + pHeaderStr) ? (blockedOpcodes.get("R_" + pHeaderStr)) : blockDefault;
        }
        return blockedOpcodes.containsKey("R_" + op) ? (blockedOpcodes.get("R_" + op)) : blockDefault;
    }

    public static void OpenIP() {
        IsTrueIP = true;
    }

    public static void OpenMAC() {
        IsTrueMAC = true;
    }

    public static boolean IsTrueIP() {
        return IsTrueIP;
    }

    public static boolean IsTrueMAC() {
        return IsTrueMAC;
    }

    public static boolean IsTrue() {
        return (IsTrueMAC) && (IsTrueIP);
    }

    public static String getProperty(String s) {
        return props.getProperty(s);
    }

    public static boolean IsLvkejian() {
        if (Boolean.parseBoolean(props.getProperty("world.lvkejian", "false"))) {
            lvkejian = true;
        }
        return lvkejian;
    }

    /*
     public static boolean 开放神之子创建() {
     return Boolean.parseBoolean(props.getProperty("world.神之子", "false"));
     }

     public static boolean 开放林之灵创建() {
     return Boolean.parseBoolean(props.getProperty("world.林之灵", "false"));
     }
     */
    public static boolean IsBossDamage() {
        return Boolean.parseBoolean(props.getProperty("world.lvkejian", "true"));
    }

    public static int get测试数据() {
        loadSettings();
        return csm;
    }

    public static int getMaplewingMAX() {
        loadSettings();
        return max;
    }

    public static void setProperty(String prop, String newInf) {
        props.setProperty(prop, newInf);
    }

    public static String getProperty(String s, String def) {
        //*去除验证
        if ((!ServerConstants.getIsTrue()) && (!Start.isTrue())) {
            System.exit(0);
        }

        return props.getProperty(s, def);
    }

    static {
        String toLoad = "world.properties";
        loadProperties(toLoad);
        try {
            PreparedStatement ps = DatabaseConnection.getConnection().prepareStatement("SELECT * FROM auth_server_channel_ip");
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                props.put(rs.getString("name") + rs.getInt("channelid"), rs.getString("value"));
            }
            rs.close();
            ps.close();
        } catch (SQLException ex) {
            System.exit(0);
        }
        if (isLoadShow()) {
            loadSettings();
        }
    }
    
}
